#coding=utf8
__author__ = 'alex'
from flask import Blueprint, render_template, abort, g, request, redirect, url_for, session, flash, send_file
from utils import *
from biz import *
from models import *

index = Blueprint('index', __name__,template_folder='templates',url_prefix='/')

@index.app_template_filter(name="content")
def show_content(document):
    if document:
        lines=document.content.split('\n')
        if lines[0].startswith("http://"):
            url=lines[0]
            content = u"\n".join(lines[1:])
        else:
            url=None
            content = document.content
        return render_template("include/content.html",**locals())
    else:
        return ""

@index.app_template_filter(name="one")
def one(doc_list):
    if doc_list:
        return doc_list[0]
    return None

@index.app_template_filter(name="nav")
def menu(txt):
    catalogs = top_level()
    return render_template("include/top_nav.html",**locals())

@index.route("")
def front():
    return render_template("front.html",**locals())

@index.route("<page_name>.html")
def page(page_name):
    return render_template("%s.html"%page_name,**locals())

@index.route("doc/<catalog_id>.html")
def catalog(catalog_id):
    catalog = g.db.query(Catalogs).get(catalog_id)
    return  render_template("document.html",**locals())